博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP丶新闻发布会系统
阅读量:6821 次
发布时间:2019-06-26

本文共 8054 字,大约阅读时间需要 26 分钟。

新闻发布会

项目所需要的一些实现类 servlet 工具类

 

1.实现登录功能

 

前端界面的代码

 

 

 

1 
2
3
4
5
6
7
8
9

登录实现类代码

1 public boolean loginGetBool(Admin admin) { 2       rs=  executeSelect("select *from admin where name=? and \"pwd\"=?",admin.getAname(),admin.getApwd()); 3       try { 4         if(rs.next()){ 5               return true; 6           } 7       } catch (SQLException e) { 8         // TODO Auto-generated catch block 9         e.printStackTrace();10       }11       return false;12     }

登录servlet

public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //接收请求时的编码utf-8          request.setCharacterEncoding("utf-8");          response.setContentType("text/html;charset=utf-8");          String  name=request.getParameter("uname");          String  pwd=request.getParameter("upwd");                    Admin admin=new Admin(name,pwd);                System.out.println(admin.getAname());                              AdminDaoImpl adi=new AdminDaoImpl();                String dbn=adi.login(admin);                             if(dbn!=null){                                         Cookie cookie=new Cookie("unameCookie",name);                    cookie.setMaxAge(60*60*24);                                        response.addCookie(cookie);                                    System.out.println("登陆成功!");                    HttpSession session= request.getSession();                    session.setAttribute("uname", name);                    session.setMaxInactiveInterval(60*10);                                        response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");                }else{                    response.sendRedirect(request.getContextPath()+"/index.jsp");                }    }

2.实现新增新闻

 

新增实现类方法

1 public boolean addNews(News news) { 2         Date date=new Date(); 3         DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4         Date time = null; 5         try { 6             time = format.parse(format.format(date)); 7         } catch (ParseException e) { 8             // TODO Auto-generated catch block 9             e.printStackTrace();10         }11         12             Object[] obj={news.getNauthor(),news.getNcontent(),time,null,news.getNtitle(),news.getNtypeid()};13     14         15       16       return  executeUpdate("INSERT INTO newsrecord (`nauthor`,`ncontent`,`startTime`,`endUpdateTime`,`ntitle`,`ntypeid`) values(?,?,?,?,?,?)",obj);17         18       19     }
View Code

新增servlet

1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3            4            request.setCharacterEncoding("utf-8"); 5            response.setContentType("text/html; charset=utf-8"); 6            //主题 7            int ntid=Integer.parseInt(request.getParameter("ntid")); 8          //标题 9            String ntitle=request.getParameter("ntitle");10           //作者 11           String nauthor=request.getParameter("nauthor");12           //摘要 13           String nsummary=request.getParameter("nsummary");14         //内容15           String ncontent=request.getParameter("nauthor");16         //上传图片17           18           19           String file=request.getParameter("file");  20           NewsWeb news=new NewsWeb(nauthor,ncontent,file,ntitle,ntid,nsummary);21           NewsWebDaoImpl nw=new  NewsWebDaoImpl();22           23           if(nw.addNewsWeb(news)){24               System.out.print("成功!");25               request.getSession().setAttribute("xi", "<>");26               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");27              // out.print("");28           }else{29               System.out.print("失败!");30              request.getSession().setAttribute("xi", "<>");31               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");32           }
新增Servlet
动态显示新闻标题内容
 
动态显示Servlet
1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3             TopicDaoImpl dao=new    TopicDaoImpl(); 4              5             List
alltopic=dao.getAllTopic(); 6 7 request.setAttribute("Topiclist", alltopic); 8 String data=request.getParameter("tid"); 9 10 if (data!=null&&!data.equals("")) {11 int tid=Integer.parseInt(data);12 13 NewDaoImpl topicdao=new NewDaoImpl();14 List
list = topicdao.getNewsById(tid);15 16 request.setAttribute("newsList",list);17 18 }else {19 //处理新闻相关内容20 NewDaoImpl newsDao=new NewDaoImpl();21 List
newsList = newsDao.getTopNews();22 request.setAttribute("newsList", newsList);23 }24 //转向DoIndexServlet获取数据25 26 //准发到index.jsp27 request.getRequestDispatcher("/index.jsp").forward(request, response);28 }
View Code

动态显示实现类

1 public List
getAllTopic() { 2 Connection connection=getConnection(); 3 String sqlString="select typeid,typename from type"; 4 QueryRunner query=new QueryRunner(); 5 List
list=null; 6 try { 7 8 list=query.query(connection, sqlString, new BeanListHandler
(NewsType.class)); 9 10 System.out.println(list.get(0).getTypeName());11 } catch (SQLException e) {12 // TODO Auto-generated catch block13 e.printStackTrace();14 }15 return list;16 }17 18 19 public List
getTopNews() {20 Connection connection=getConnection();21 QueryRunner query=new QueryRunner();22 //select * from newsrecord where rownum<=3 orcal查询前三条语句23 String sqlString="select * from newsrecord where nid limit 3";24 List
list=null;25 try {26 list=query.query(connection, sqlString, new BeanListHandler
(News.class));27 } catch (SQLException e) {28 // TODO Auto-generated catch block29 e.printStackTrace();30 }31 return list;32 }

前端代码

1  
2
12
19
View Code
添加新闻主题

新增新闻类型Servlet
1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3  4           request.setCharacterEncoding("utf-8"); 5           response.setContentType("text/html; charset=utf-8"); 6           String tname=request.getParameter("tname"); 7           NewsType newsType=new NewsType(tname); 8           NewsTypeDaoImpl ntdi=new NewsTypeDaoImpl(); 9           if (ntdi.addNewsType(newsType)) {10               request.getSession().setAttribute("xi", "<>");11           }else{12               System.out.println("");13               request.getSession().setAttribute("xi", "<>");14           }15           response.sendRedirect("/news/util/addnewstype.jsp");16           17     }
View Code

新增类型实现类

1 public boolean addNewsType(NewsType newsType){    2       return    executeUpdate("insert into type(typename)   3            values(?)", newsType.getTypeName());4     }
View Code

 

  

 

转载于:https://www.cnblogs.com/PGYXZ/p/4996244.html

你可能感兴趣的文章
jQuery事件之自定义事件
查看>>
iOS小Tip之查看FPS
查看>>
最近点对-分治
查看>>
vagrant特性——基于docker开发环境(docker和vagrant的结合)-1-基本使用
查看>>
【ZZ】技能表合集
查看>>
Vijos P1756 数字反转【进制】
查看>>
jQuery的性能优化
查看>>
简易付弹窗问题FAQ
查看>>
Oracle存储过程语法
查看>>
[springBoot系列]--springBoot注解大全
查看>>
c程序设计语言第一章1
查看>>
java_IO读写模版
查看>>
删除某个文件夹下的所有文件
查看>>
高精度模板
查看>>
python-第三方模块
查看>>
C 基础
查看>>
关于使用Cocos2dx定制动态进度条的实现启发——继承自CCActionInterval
查看>>
Mysql创建函数出错
查看>>
字符串作业2
查看>>
ArrayList和LinkedList 内部结构分析(一)
查看>>